package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.PrisonerAccountDao;
import com.zoshine.zzg.dao.PrisonerCapitalFlowDao;
import com.zoshine.zzg.dao.PrisonerSettlementDao;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.PrisonerAccount;
import com.zoshine.zzg.pojo.PrisonerCapitalFlow;
import com.zoshine.zzg.pojo.PrisonerSettlement;
import com.zoshine.zzg.pojo.pojostatus.PrisonerCapitalFlowStatus;
import com.zoshine.zzg.pojo.pojostatus.PrisonerSettlementStatus;
import com.zoshine.zzg.pojo.pojostatus.SettlementStatus;
import com.zoshine.zzg.util.idworker.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 犯人结算Service
 * @author 田沛凌
 * @time 2017年7月10日 上午9:57:40
 */
@Service
@Transactional
public class PrisonerSettlementService extends BaseService<PrisonerSettlement> {
    private IdWorker<Long> settlementIdWork;
    private PrisonerSettlementDao prisonerSettlementDao;
    private PrisonerCapitalFlowDao prisonerCapitalFlowDao;
    private PrisonerAccountDao prisonerAccountDao;


    @Autowired
    public void setPrisonerSettlementDao(PrisonerSettlementDao prisonerSettlementDao) {
        this.prisonerSettlementDao = prisonerSettlementDao;
    }


    @Autowired
    public void setSettlementIdWork(IdWorker<Long> settlementIdWork) {
        this.settlementIdWork = settlementIdWork;
    }


    @Autowired
    public void setPrisonerCapitalFlowDao(PrisonerCapitalFlowDao prisonerCapitalFlowDao) {
        this.prisonerCapitalFlowDao = prisonerCapitalFlowDao;
    }


    @Autowired
    public void setPrisonerAccountDao(PrisonerAccountDao prisonerAccountDao) {
        this.prisonerAccountDao = prisonerAccountDao;
    }

    /**
     * 犯人结算提交转账
     * @param idStr 结算ID
     * @return
     */

    public void settlementtoflow(String idStr){
        //为空判断
        if (idStr == null || idStr.length()==0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }

        String[] pidArr = idStr.split(",");
        List<Long> ids = new ArrayList<>();
        for (String id : pidArr) {

            System.out.println(id);
            PrisonerSettlement prisonerSettlement = prisonerSettlementDao.selectByPrimaryKey(Long.valueOf(id));
            //犯人结算状态 完成结算
            prisonerSettlement.setStatus(PrisonerSettlementStatus.PRESENTING_BANK.getValue());
            prisonerSettlementDao.update(prisonerSettlement);
            //查询犯人信息
            PrisonerAccount prisonerAccount = prisonerAccountDao.selectByPrimaryKey(prisonerSettlement.getPrisonerAccount());
            //增加犯人资金流水
            PrisonerCapitalFlow prisonerCapitalFlow = new PrisonerCapitalFlow();
            prisonerCapitalFlow.setStatus(PrisonerCapitalFlowStatus.DELIVERY_BANK.getValue());
            prisonerCapitalFlow.setBankCard(prisonerAccount.getBankCard());
            prisonerCapitalFlow.setFlowType(Long.parseLong("23780913574568959"));
            prisonerCapitalFlow.setFlowMoney(prisonerSettlement.getSettlementMoney());
            prisonerCapitalFlow.setPrisonerId(prisonerSettlement.getPrisonerAccount());
            prisonerCapitalFlow.setId(settlementIdWork.createId());
            prisonerCapitalFlow.setCreateTime(new Date());
            prisonerCapitalFlow.setUpdateTime(new Date());
            prisonerCapitalFlowDao.insert(prisonerCapitalFlow);
        }





    }




//    @Override
//    public void delPrisonerSettlement(Long settlementId) {
//        if(settlementId == null){
//            throw new AppException(ErrorMessage.SETTLEMENT_ID_CAN_NOT_NULL);
//        }
//        prisonerSettlementDao.delete(settlementId);
//    }
//
//
//    @Override
//    public void addPrisonerSettlement(PrisonerSettlement prisonerSettlement) {
//
//        prisonerSettlement.setSettlementId(settlementIdWork.createId());
//        prisonerSettlement.setStatus(SettlementStatus.INVALID.getValue());
//        prisonerSettlement.setCreateTime(new Date());
//        prisonerSettlement.setUpdateTime(new Date());
//        prisonerSettlementDao.insert(prisonerSettlement);
//    }
//
//
//    @Override
//    public PrisonerSettlement getPrisonerSettlement(Long settlementId) {
//        if(settlementId == null){
//            throw new AppException(ErrorMessage.SETTLEMENT_ID_CAN_NOT_NULL);
//        }
//        return prisonerSettlementDao.selectOne(settlementId);
//    }
//
//
//    @Override
//    public void editPrisonerSettlement(PrisonerSettlement prisonerSettlement) {
//        prisonerSettlementDao.update(prisonerSettlement);
//    }
//
//
//    @Override
//    public List<PrisonerSettlement> listPrisonerSettlement(PrisonerSettlement prisonerSettlement) {
//        return prisonerSettlementDao.selectList(prisonerSettlement);
//    }



   
}
